class Thematic < ActiveRecord::Base
  has_one :category
  has_many :materials
  has_many :profiles, :as => :reference

  def self.search_selector(search_name, page)
    items_per_page = count_record_per_page :thematics

    if I18n.locale == 'es'
      order = "name_web_es"
      attr_name = "name_web_es"
    else
      order = "name_web_en"
      attr_name = "name_web_en"
    end
    
    conditions_clauses = []
    conditions_options = []

    if !search_name.nil? && search_name != ""
      conditions_clauses << "upper(#{attr_name}) like upper(?)"
      conditions_options << "%#{search_name}%"
    end
    conditions = [conditions_clauses.join(" AND "), conditions_options].flatten

    @thematics = paginate :per_page => items_per_page,:page => page, :conditions => conditions, :order => order
    return @thematics
  end

  def name_to_local_language
    if I18n.locale == 'es'
      return name_web_es
    else
      return name_web_en
    end
  end
  
end
